define(['modules/forms/module', 'x-editable'], function (module) {
    "use strict";

    return module.registerDirective('smartXeditable', function($timeout, $log){

    	function link (scope, element, attrs, ngModel) {

            var defaults = {
                // display: function(value, srcData) {
                //     ngModel.$setViewValue(value);
                //     // scope.$apply();
                // }
            };

            var inited = false;

            var initXeditable = function() {

                var options = scope.options || {};
        		var initOptions = angular.extend(defaults, options);
                $log.log(element);
                element.editable('destroy');
                element.editable(initOptions);
            }

            scope.$watch("options", function(newValue) {

                if(!newValue) {
                    return false;
                }

                initXeditable();

                // $log.log("Options changed...");

            }, true);
            scope.$watch("ngBind", function(newValue) {
                if(newValue){
                    element.handleEmpty
                }

            });
        }

        return {
        	restrict: 'A',
        	require: "ngModel",
            scope: {
                options: "=",
                ngBind: "="
            },
        	link: link 

        }
    })
});